<?php

Yii::import('application.models._base.BaseCategory');


class Category extends BaseCategory
{
	public $keywords;
	
	public function rules() {
		return array(
				array('title, created', 'required'),
				array('title','unique'),
				array('created', 'numerical', 'integerOnly'=>true),
				array('title, meta_title, meta_keywords, url', 'length', 'max'=>255),
				array('description, meta_description', 'safe'),
				array('keywords','safe'),
				array('id, title, created, description, meta_title, meta_keywords, meta_description, url', 'safe', 'on'=>'search'),
		);
	}
	
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	
	public function relations() {
		return array(
			'productInfo'=>array(self::MANY_MANY,'ProductInfo','category_relations(category_id,product_id)'),
		);
	}
	
	
	public function beforeSave(){
		if(!empty($this->url)){
			$this->url=urlencode($this->url);
		}
		if(!empty($this->description) && empty($this->meta_description)){
			$this->meta_description=$this->description;
		}
		if(empty($this->meta_title)){
			$this->meta_title=$this->title;
		}
		return true;
	}
	
	public function search() {
		$criteria = new CDbCriteria;
	    $criteria->with=array('productInfo');
		/* $criteria->together=true;  */
	    //$criteria->compare('t.id', $this->with('productInfo')->id);
	    $criteria->compare('t.id', $this->id);
		if(!empty($this->keywords)){
			$criteria->compare('t.title', $this->keywords, true,'OR');
		} 
		return new CActiveDataProvider($this, array(
				'criteria' => $criteria,
		));
	}
}